package com.sign2024.sign2024project.projectservice.service.impl;

import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.GcUpdatePlan;
import com.sign2024.sign2024core.entry.projectservice.GcUpdateProject;
import com.sign2024.sign2024core.entry.projectservice.HistoricalRelational;
import com.sign2024.sign2024core.entry.projectservice.SpecialtySignRelation;
import com.sign2024.sign2024core.entry.projecttask.RollbackRecord;
import com.sign2024.sign2024project.projectservice.mapper.GcUpdateDataMapper;
import com.sign2024.sign2024project.projectservice.mapper.SpecialtySignRelationMapper;
import com.sign2024.sign2024project.projectservice.service.GcUpdateDataService;
import com.sign2024.sign2024project.projectservice.vo.GcUpdateVo;
import com.sign2024.sign2024project.projecttask.mapper.RollbackRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2024/9/10 14:10
 */
@Service
public class GcUpdateDataServiceImpl implements GcUpdateDataService {

    @Resource
    private GcUpdateDataMapper gcUpdateDataMapper;

    @Resource
    private SpecialtySignRelationMapper specialtySignRelationMapper;

    @Resource
    private RollbackRecordMapper rollbackRecordMapper;

    @Override
    public void addGcUpdateProjectList(List<GcUpdateProject> list, String fileName) {
        if (list.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        } else {
            gcUpdateDataMapper.cleanGcUpdateProject();
            for (GcUpdateProject gc : list) {
                gc.setFileName(fileName);
                gcUpdateDataMapper.insertGcUpdateProject(gc);
            }
        }
    }

    @Override
    public void addGcUpdatePlanList(List<GcUpdatePlan> list, String fileName) {
        if (list.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        } else {
            gcUpdateDataMapper.cleanGcUpdatePlan();
            for (GcUpdatePlan gc : list) {
                gc.setFileName(fileName);
                gcUpdateDataMapper.insertGcUpdatePlan(gc);
            }
        }
    }

    @Override
    public void saveGcUpdate(Integer taskState) {
        // 判断 数据 是否存在 计划清单 抽取方案
        List<GcUpdatePlan> planList = gcUpdateDataMapper.selectGcUpdatePlanList();
        if (planList.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请导入计划清单");
        }

        List<GcUpdateProject> projectList = gcUpdateDataMapper.selectGcUpdateProjectList();
        if (projectList.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请导入抽取方案");
        }

        // 将 当前数据 进行 历史版本留存
        // 获取历史版本号
        int code = 0;
        Integer versionCode = specialtySignRelationMapper.countMaxVersionCode();
        if (versionCode != null) {
            code = versionCode + 1;
        } else {
            code = code + 1;
        }

        // 获取 当前 关系表中的数据
        List<SpecialtySignRelation> list = specialtySignRelationMapper.selectAllList();
        if (!list.isEmpty()) {
            for (SpecialtySignRelation ss :
                    list) {
                HistoricalRelational h = new HistoricalRelational();
                // 版本号是1
                h.setVersionCode(code);
                h.setMaterialCode(ss.getMaterialCode());
                h.setMaterialDescribe(ss.getMaterialDescribe());
                h.setExtendDescribe(ss.getExtendDescribe());
                h.setSignMajorOne(ss.getSignMajorOne());
                h.setSignMajorTwo(ss.getSignMajorTwo());
                h.setSignMajorThree(ss.getSignMajorThree());
                h.setSignMajorFour(ss.getSignMajorFour());
                specialtySignRelationMapper.insertHistoricalRelational(h);
            }
        }

        // 获取 更新数据
        List<GcUpdateVo> voList = gcUpdateDataMapper.selectUpdateData();
        if (!voList.isEmpty()) {
            for (GcUpdateVo vo : voList) {
                // 根据 编码 进行 数据查询
                SpecialtySignRelation relation = specialtySignRelationMapper.selectRelationByCode(vo.getSuppliesCode());
                if (StringUtils.isNotNull(relation)) {
                    // 不为空 数据 更新
                    // 没有数据 修改， 修改到专家专业 1 ，然后把原有的 1 放到 2， 2到3 一次类推
                    SpecialtySignRelation specialtySignRelation = new SpecialtySignRelation();
                    specialtySignRelation.setId(relation.getId());
                    specialtySignRelation.setSignMajorOne(vo.getSignMajor());
                    // 1 - 2
                    if (StringUtils.isNotNull(relation.getSignMajorOne())) {
                        specialtySignRelation.setSignMajorTwo(relation.getSignMajorOne());
                    }
                    // 2 - 3
                    if (StringUtils.isNotNull(relation.getSignMajorTwo())) {
                        specialtySignRelation.setSignMajorThree(relation.getSignMajorTwo());
                    }

                    // 3 - 4
                    if (StringUtils.isNotNull(relation.getSignMajorThree())) {
                        specialtySignRelation.setSignMajorFour(relation.getSignMajorThree());
                    }

                    specialtySignRelationMapper.updateSpecialtySignRelation(specialtySignRelation);

                } else {
                    // 编码不存在 进行数据添加 专家专业 1
                    SpecialtySignRelation sign = new SpecialtySignRelation();
                    // 编码
                    sign.setMaterialCode(vo.getSuppliesCode());
                    // 专业
                    sign.setSignMajorOne(vo.getSignMajor());
                    specialtySignRelationMapper.insertSpecialtySignRelation(sign);
                }
            }
        }

        // 添加 回退记录  taskState 任务标识
        RollbackRecord rollback = new RollbackRecord();
        // 文件名
        String planName = planList.get(0).getFileName();
        String projectName = projectList.get(0).getFileName();

//        String fileName = planName + "," + projectName;
        rollback.setFileName(projectName);
        rollback.setFileNameTwo(planName);
        // 任务标识
        rollback.setTaskState(taskState);
        // 数据 版本号
        rollback.setVersionCode(code);
        rollbackRecordMapper.insert(rollback);


        // 数据 更新完成 清空数据
        gcUpdateDataMapper.cleanGcUpdatePlan();
        gcUpdateDataMapper.cleanGcUpdateProject();
    }
}
